package server;

import java.io.IOException;
import java.io.InputStream;

/**
 * 把请求信息封装为Request对象（根据InputStream输入流封装）
 * @author chengzhj
 * @data 2021/8/28 4:03 下午
 */
public class Request {

    private String method; // 请求方法
    private String url; // 请求的url
    private String hostName;

    private InputStream inputStream;

    public String getMethod() {
        return method;
    }

    public void setMethod(String method) {
        this.method = method;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public InputStream getInputStream() {
        return inputStream;
    }

    public void setInputStream(InputStream inputStream) {
        this.inputStream = inputStream;
    }

    public String getHostName() {
        return hostName;
    }

    public void setHostName(String hostName) {
        this.hostName = hostName;
    }

    public Request() {
    }

    // 构造器，输入流传入
    public Request(InputStream inputStream) throws IOException {
        this.inputStream = inputStream;

        // 从输入流中获取请求信息
        int count = 0;
        while (count == 0) {
            count = inputStream.available();
        }

        byte[] bytes = new byte[count];
        // 将输入流信息写入bytes
        inputStream.read(bytes);

        String inputStr = new String(bytes);
        // 获取第一行请求头信息
        String firstLine = inputStr.split("\\n")[0];
        String[] strings = firstLine.split(" ");

        this.method = strings[0];
        this.url = strings[1];
        this.hostName = inputStr.split("\\n")[1].split(":")[1].replace(" ", "");

        // System.out.println("请求信息：----------" + inputStr);
        System.out.println("------->method:" + method);
        System.out.println("======>url:" + url);
        System.out.println("******>hostName:" + hostName);
    }
}
